package com.gitee.huanminabc.dynamicdb.filters;

import com.gitee.huanminabc.dynamicdb.common.Constants;
import com.gitee.huanminabc.dynamicdb.common.DynamicDbException;
import com.gitee.huanminabc.dynamicdb.common.SpringContextHolder;
import com.gitee.huanminabc.dynamicdb.config.DynamicDbSpringConfig;
import com.gitee.huanminabc.dynamicdb.core.DBContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Slf4j
//import org.apache.dubbo.registry.Constants;
//@Activate(order = 999, group = {Constants.PROVIDER_PROTOCOL, Constants.CONSUMER_PROTOCOL})
@Activate(order = Integer.MIN_VALUE, group = {CommonConstants.PROVIDER})
//服务端接收到客户端请求的前置拦截器
public class ProviderDubboDynamicDbFilter implements Filter {
    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
//        log.info("ProviderDubboDynamicDbFilter");
        DynamicDbSpringConfig dynamicDbSpringConfig =(DynamicDbSpringConfig) SpringContextHolder.getBean(DynamicDbSpringConfig.class);
        RpcContext context = RpcContext.getContext();
        if (dynamicDbSpringConfig.isMultiTenantMode()) {
            String tenantKey = context.getAttachment(Constants.TENANT_KEY);
            if (tenantKey != null) {
                DBContext.tenantKeys.set(tenantKey);
            }
        }
        return invoker.invoke(invocation);
    }
}